﻿/****************************** Module Header ******************************\
 * Module Name:  ExMethodForDropDownList.cs
 * Module Version:  1.0.0 (2011-9-30)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used to extension webcontrol instance's method.  It supplies 
 * public static extension methods to load data or get othor control.
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/

using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace UCF.Core.Web
{
    /// <summary>
    /// Control服务器控件的扩展方法
    /// </summary>
    public static class ExMethodForControl
    {
        /// <summary>
        /// 设置值
        /// </summary>
        /// <param name="c"></param>
        /// <param name="v"></param>
        public static void SetControlValue(this Control c, string v)
        {
            if (c.GetType() == typeof(TextBox))
            {
                TextBox o = c as TextBox;
                if (o.TextMode == TextBoxMode.Password)
                {
                    o.SetPassword(v);
                }
                else
                {
                    o.Text = v;
                }
                return;
            }
            if (c.GetType() == typeof(HtmlInputText) || c.GetType() == typeof(HtmlInputPassword) || c.GetType() == typeof(HtmlTextArea))
            {
                ((HtmlInputText)c).Value = v;
                return;
            }
        }

        /// <summary>
        /// 设置勾选状态
        /// </summary>
        /// <param name="c"></param>
        /// <param name="v"></param>
        public static void SetControlChecedState(this Control c, bool v)
        {
            if (c.GetType() == typeof(CheckBox))
            {
                ((CheckBox)c).Checked = v;
                return;
            }
            if (c.GetType() == typeof(HtmlInputCheckBox))
            {
                ((HtmlInputCheckBox)c).Checked = v;
                return;
            }
        }
        /// <summary>
        /// 获取所有子控件
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public static IList<Control> GetChildControls(this Control c, int deep = 99)
        {
            IList<Control> list = new List<Control>();
            c.GetChildControlsFnc(ref list,deep);
            return list;
        }
        /// <summary>
        /// 获取所有子控件
        /// </summary>
        /// <param name="c"></param>
        /// <param name="cc"></param>
        private static void GetChildControlsFnc(this Control c, ref IList<Control> cc, int deep = 99, int loopcount = 0)
        {
            if (loopcount >= deep) return;
            foreach (Control sc in c.Controls)
            {
                if (sc.GetType().Namespace!="System.Web.UI.HtmlControls")
                {
                    cc.Add(sc);
                }
                if (sc.HasControls()) sc.GetChildControlsFnc(ref cc, deep, loopcount + 1);
            }
        }

        /// <summary>
        /// 将控件集合转换成列表集合
        /// </summary>
        /// <param name="cc"></param>
        /// <returns></returns>
        public static IList<Control> ToList(this ControlCollection cc)
        {
            IList<Control> list = new List<Control>();
            foreach (Control c in cc)
            {
                list.Add(c);
            }
            return list;
        }
    }
}
